/*
  ******************************************************************************
  * @file    apt32f172_tc2_st16.c
  * @author  APT AE Team
  * @version V1.12
  * @date    2019/03/08
  ******************************************************************************
  *THIS SOFTWARE WHICH IS FOR ILLUSTRATIVE PURPOSES ONLY WHICH PROVIDES 
  *CUSTOMER WITH CODING INFORMATION REGARDING THEIR PRODUCTS.
  *APT CHIP SHALL NOT BE HELD RESPONSIBILITY ADN LIABILITY FOR ANY DIRECT, 
  *INDIRECT DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF 
  *SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION 
  *CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.AND APT CHIP RESERVES 
  *THE RIGHT TO MAKE CHANGES IN THE SOFTWARE WITHOUT NOTIFICATION
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/ 
#include "apt32f172_tc2_st16.h"
/* defines -------------------------------------------------------------------*/

/* externs--------------------------------------------------------------------*/


/*************************************************************/
//STC16 RESET VALUE
//EntryParameter:
//ReturnValue:NONE
/*************************************************************/  
void STC16_RESET_VALUE(void)									//reset value
{
  	ST16->IDR=STC_IDR_RST;          			/**< IDR reset value      */
   	ST16->CEDR=STC_CEDR_RST;		 			/**< CEDR reset value     */	
   	ST16->RSSR=STC_RSSR_RST;					/**< RSSR reset value     */	
   	ST16->IMSCR=STC_IMSCR_RST;					/**< IMSCR reset value    */	
   	ST16->RISR=STC_RISR_RST;					/**< RISR reset value     */	
  	ST16->MISR=STC_MISR_RST;					/**< MISR reset value     */	
   	ST16->ICR=STC_ICR_RST;						/**< ICR reset value      */	
   	ST16->SR=STC_SR_RST;						/**< SR reset value       */	
   	ST16->MR=STC_MR_RST;						/**< MR reset value       */	
  	ST16->CNTBR=STC_CNTBR_RST;					/**< CNTBR reset value    */
   	ST16->CNTR=STC_CNTR_RST;					/**< CNTR reset value     */
   	ST16->CDR=STC_CDR_RST;						/**< CDR reset value      */
   	ST16->PCNTR=STC_PCNTR_RST;					/**< PCNTR reset value    */
   	ST16->CRR=STC_CRR_RST;						/**< CRR reset value      */
   	ST16->CMR=STC_CMR_RST;						/**< CMR reset value      */
  	ST16->CIMSCR=STC_CIMSCR_RST;				/**< CIMSCR reset value   */	
   	ST16->CRISR=STC_CRISR_RST;					/**< CRISR reset value    */	
   	ST16->CMISR=STC_CMISR_RST;					/**< CMISR reset value    */
   	ST16->CICR=STC_CICR_RST;					/**< CICR reset value     */	
   	ST16->CAPSR=STC_CAPSR_RST;					/**< CAPSR reset value    */
   	ST16->CC0R=STC_CC0R_RST;					/**< CC0R reset value     */
   	ST16->CC1R=STC_CC1R_RST;					/**< CC1R reset value     */
}
/*************************************************************/
//STC16 IO Init
//EntryParameter:STC16_IO_MODE_X,STC16_IO_G
//STC16_IO_MODE_X:STC16_IO_CAP0,STC16_IO_CAP1
//STC16_IO_CAP0(0->PA0.7(AF2);1->PA1.0(AF1))
//STC16_IO_CAP1(0->PA1.2(AF1))
//ReturnValue:NONE
/*************************************************************/
void STC16_IO_Init(ST16_IO_MODE_TypeDef  STC16_IO_MODE_X , U8_T STC16_IO_G )
{
	if(STC16_IO_MODE_X==STC16_IO_CAP0)
	{
		if(STC16_IO_G==0)
		{
			GPIOA0->CONLR=(GPIOA0->CONLR & 0X0FFFFFFF)|0x50000000;										//STC16_IO_CAP0(PA0.7->AF2)
		}
		else if(STC16_IO_G==1)
		{
			GPIOA1->CONLR=(GPIOA1->CONLR & 0XFFFFFFF0)|0x00000004;										//STC16_IO_CAP0(PA1.0->AF1)
		}
	}
	else if(STC16_IO_MODE_X==STC16_IO_CAP1)
	{
		if(STC16_IO_G==0)
		{
			GPIOA1->CONLR=(GPIOA1->CONLR & 0XFFFFF0FF)|0x00000400;										//STC16_IO_CAP0(PA1.2->AF1)
		}
	}
}
/*************************************************************/
//STC16 CLK CMD
//EntryParameter:NewState
//NewState:DISABLE,ENABLE
//ReturnValue:NONE
/*************************************************************/
void STC16_Clk_CMD(FunctionalStatus NewState)
{
	if(NewState != DISABLE)
	{
		ST16->CEDR |= 0x01;							//enable STC16 clk 
	}
	else
	{
		ST16->CEDR &= 0XFFFFFFFE;					//Disable STC16 clk 
	}
}
/*************************************************************/
//STC16 Softreset
//EntryParameter:
//ReturnValue:NONE
/*************************************************************/ 
void STC16_Softreset(void)
{
	ST16->RSSR|=0x80000000;
}
/*************************************************************/
//STC16 start
//EntryParameter:
//ReturnValue:NONE
/*************************************************************/ 
void STC16_Start(void)
{
	ST16->RSSR|=0X01;									//STC16 start
	while(!(ST16->SR&0X80000000));						//计数器正在工作
}
/*************************************************************/
//STC16 stop
//EntryParameter:
//ReturnValue:NONE
/*************************************************************/ 
void STC16_stop(void)
{
	ST16->RSSR|=0X02;									//STC16 stop
	while(ST16->SR&0X80000000);							//计数器没有工作
}
/*************************************************************/
//STC16 Channel0 CMD
//EntryParameter:NewState
//NewState:DISABLE,ENABLE
//ReturnValue:NONE
/*************************************************************/ 
void STC16_Channel0_CMD(FunctionalStatus NewState)
{
	if(NewState != DISABLE)
	{
		ST16->CRR |= 0x01;							//enable STC16 Channel0 clk 
	}
	else
	{
		ST16->CRR &= 0XFFFFFFFE;						//Disable STC16 Channel0 clk 
	}
}
/*************************************************************/
//STC16 Channel1 CMD
//EntryParameter:
//ReturnValue:NONE
/*************************************************************/ 
void STC16_Channel1_CMD(FunctionalStatus NewState)
{
	if(NewState != DISABLE)
	{
		ST16->CRR |= 0x02;							//enable STC16 Channel1 clk 
	}
	else
	{
		ST16->CRR &= 0XFFFFFFFD;						//Disable STC16 Channel1 clk 
	}
}
/*************************************************************/
//STC16 Match INT Configure
//EntryParameter:STC16_MINT_X,NewState
//STC16_MINT_X:ST16_STARTI,ST16_STOPI,ST16_PENDI,
//NewState:ENABLE,DISABLE
//ReturnValue:NONE
/*************************************************************/ 
void STC16_MINT_CMD(STC16_MINT_TypeDef STC16_MINT_X , FunctionalStatus NewState)
{
	if (NewState != DISABLE)
	{
		ST16->IMSCR |= STC16_MINT_X;					//SET
	}
	else
	{
		ST16->IMSCR &= ~ STC16_MINT_X;				//CLR
	}
}
/*************************************************************/
//STC16 Capture INT Configure
//EntryParameter:STC16_CINT_X,NewState
//STC16_CINT_X:ST16_CC0RI,ST16_CC1RI,ST16_CC0FI,ST16_CC1FI,ST16_Match0,ST16_Match1
//NewState:ENABLE,DISABLE
//ReturnValue:NONE
/*************************************************************/ 
void STC16_CINT_CMD(STC16_CINT_TypeDef STC16_CINT_X , FunctionalStatus NewState)
{
	if (NewState != DISABLE)
	{
		ST16->CIMSCR |=STC16_CINT_X;					//SET
	}
	else
	{
		ST16->CIMSCR &=(~STC16_CINT_X);				//CLR
	}
}
/*************************************************************/
//STC16 Configure 
//EntryParameter:STC16_Count_mode_X,STC16_Count_STOPTYPE,STC16_CM0_Mode_X,STC16_CM1_Mode_X,STC16_DIVM,STC16_DIVN
//STC16_Count_mode_X:STC16_Count_mode_Continue,STC16_Count_mode_Oneshot
//STC16_Count_STOPTYPE:STC16_Count_STOPTYPE_AutoLoad,STC16_Count_STOPTYPE_StopConmand
//STC16_CM0_Mode_X:STC16_CM0_Mode_Capture,STC16_CM0_Mode_Match
//STC16_CM1_Mode_X:STC16_CM1_Mode_Capture,STC16_CM1_Mode_Match
//STC16_DIVM:0~0x3ff
//STC16_DIVN:0~0x0f
//ReturnValue:NONE
/*************************************************************/
void STC16_Configure(STC16_Count_mode_TypeDef STC16_Count_mode_X , STC16_Count_STOPTYPE_TypeDef STC16_Count_STOPTYPE
					 , STC16_CM0_Mode_TypeDef STC16_CM0_Mode_X , STC16_CM1_Mode_TypeDef STC16_CM1_Mode_X , int STC16_DIVM , int STC16_DIVN)
{
    ST16->MR&=0xfefffdff;
	ST16->CDR&=0Xffff8000;
	ST16->CMR&=0XFFFFFFFA;
	ST16->MR|=STC16_Count_mode_X|STC16_Count_STOPTYPE;
	ST16->CDR|=(STC16_DIVM<<4)|STC16_DIVN;
	ST16->CMR|=STC16_CM0_Mode_X|STC16_CM1_Mode_X;
}
/*************************************************************/
//STC16 Channel0 Capture LoadMode set
//EntryParameter:STC16_C0SR_X
//STC16_C0SR_X:STC16_C0SR_CaptureRise,STC16_C0SR_CaptureFall,STC16_C0SR_CaptureRiseFall
//ReturnValue:Conter prdr register value
/*************************************************************/ 
void STC16_Channel0_Capture_LoadMode_set(STC16_C0SR_TypeDef STC16_C0SR_X)
{
	ST16->CRR &= 0XFFFFFFFE;						//Disable STC16 Channel0 clk 
	ST16->CMR&=0XFFFCFFFF;
	ST16->CMR|=STC16_C0SR_X;
	ST16->CRR |= 0x01;							//enable STC16 Channel0 clk
}
/*************************************************************/
//STC16 Channel1 Capture LoadMode set
//EntryParameter:STC16_C1SR_X
//STC16_C1SR_X:STC16_C1SR_CaptureRise,STC16_C1SR_CaptureFall,STC16_C1SR_CaptureRiseFall
//ReturnValue:Conter prdr register value
/*************************************************************/ 
void STC16_Channel1_Capture_LoadMode_set(STC16_C1SR_TypeDef STC16_C1SR_X)
{
	ST16->CRR &= 0XFFFFFFFD;						//Disable STC16 Channel0 clk 
	ST16->CMR&=0XFFF3FFFF;
	ST16->CMR|=STC16_C1SR_X;
	ST16->CRR |= 0x02;							//enable STC16 Channel0 clk
}
/*************************************************************/
//STC16 CNTR、CC0、CC1 load
//EntryParameter:loadCounter_PRDR,loadCounter_PULR
//ReturnValue:Conter prdr register value
/*************************************************************/ 
void STC16_CNR_CC0_CC1_Load(U16_T STC16_CNTR, U16_T STC16_CC0R , U16_T STC16_CC1R)
{
	ST16->CNTR=STC16_CNTR;
	ST16->CC0R=STC16_CC0R;
	ST16->CC1R=STC16_CC1R;
}
/*************************************************************/
//STC16 Interrupt enable
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/
void STC16_Int_Enable(void)
{
    INTC_ISER_WRITE(TC2_INT);    
}
/*************************************************************/
//STC16 Interrupt enable
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/
void STC16_Int_Disable(void)
{
    INTC_ICER_WRITE(TC2_INT);    
}
/*************************************************************/
//STC16 Interrupt wake up enable
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/
void STC16_Wakeup_Enable(void)
{
    INTC_IWER_WRITE(TC2_INT);    
}

/*************************************************************/
//STC16 Interrupt wake up enable
//EntryParameter:NONE
//ReturnValue:NONE
/*************************************************************/
void STC16_Wakeup_Disable(void)
{
    INTC_IWDR_WRITE(TC2_INT);    
}

/******************* (C) COPYRIGHT 2018 APT Chip *****END OF FILE****/